<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="VIP名称" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <!-- <el-table-column v-if="colData[0].istrue" min-width="40" align="center" prop="id" label="ID"></el-table-column> -->
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="name" label="VIP名称"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="vip" label="VIP等级"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="number" label="应用上传数量限制"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="130" align="center" prop="size" label="单个文件大小"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="130" align="center" prop="upgrade" label="提升"></el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdVip(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="getdeleteVip(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 隐藏 -->
    <!-- 添加dialog表单 -->
    <div>
      <el-dialog :title="editSubjects ? '添加' : '编辑'" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
        <el-form :model="addForm" ref="ruleForm" :rules="rules">
          <el-form-item label="VIP等级:" label-width="120px" prop="vip">
            <el-input v-model.number="addForm.vip" class="add_select" placeholder="请输入VIP等级" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="应用数量:" label-width="120px" prop="number">
            <el-input v-model.number="addForm.number" class="add_select" placeholder="请输入应用数理" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="单个文件大小:" label-width="120px" prop="size">
            <el-input v-model="addForm.size" class="add_select" placeholder="请输入单个文件大小" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="提升:" label-width="120px" prop="upgrade">
            <el-input v-model.number="addForm.upgrade" class="add_select" placeholder="请输入提升" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="VIP名称:" label-width="120px" prop="name">
            <el-input v-model="addForm.name" class="add_select" placeholder="请输入VIP名称" maxlength="30"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
          <el-button v-if="editSubjects" type="primary" @click="getaddVip('ruleForm')" size="mini" round>确 定</el-button>
          <el-button v-else type="primary" @click="geteditVip('ruleForm')" size="mini" round>编辑</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryVip, addVip, queryIdVip, editVip, deleteVip } from "@/api/allocation.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["VIP名称", "VIP等级", "应用上传数量限制", "单个文件大小", "提升"],
      key: 1,
      colData: [], //动态控制table
      //归属table
      browserTable: [],
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //浏览器专题id
      subjectId: 0,
      //添加表单
      addForm: {
        vip: "",
        number: "",
        size: "",
        upgrade: "",
        name: "",
        userId: localStorage.getItem("ID"),
      },
      rules: {
        vip: [{ required: true, message: "请输入VIP等级" }, { type: 'number', message: '必须为数字值' }],
        number: [{ required: true, message: "请输入应用数理" }, { type: 'number', message: '必须为数字值' }],
        size: [{ required: true, message: "请输入单个文件大小", trigger: "blur" }],
        upgrade: [{ required: true, message: "请输入提升" }, { type: 'number', message: '必须为数字值' }],
        name: [{ required: true, message: "请输入VIP名称", trigger: "blur" }]
      },
      listQuery: {
        startSize: 1,
        pageSize: 10,
        userId: localStorage.getItem("ID"),
        type: 1,
        name: undefined,
      },
      //删除弹窗
      deleteDialogs: { show: false, deleteText: '', deleteId: 0 }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryVip()
  },
  methods: {
    //删除
    getdeleteVip(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { id: this.deleteDialogs.deleteId, userId: localStorage.getItem("ID"), }
      deleteVip(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryVip()
      })
    },
    //添加VIP信息
    getaddVip(fromName) {
      this.$refs[fromName].validate(valid => {
        if (valid) {
          let add = JSON.parse(JSON.stringify(this.addForm))
          addVip(add).then(res => {
            this.$message.success(res.msg)
            this.getqueryVip()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //编辑VIP信息
    geteditVip(fromName) {
      this.$refs[fromName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.userId = localStorage.getItem("ID")
          addForm.id = this.subjectId
          editVip(addForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryVip()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //id查询信息
    getqueryIdVip(id) {
      this.editSubjects = false
      this.subjectId = id
      queryIdVip({ id: id }).then(res => {
        this.dialogFormVisible = true
        let data = res.data
        Object.keys(this.addForm).forEach(key => { this.addForm[key] = data[key] })
      })
    },
    getqueryVip(startSize = 1, pageSize = 10, type = 1, parameter) {
      let qs = { startSize: startSize, pageSize: pageSize, type: type, ...parameter };
      queryVip(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      })
    },
    //搜索,1:名称
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.type = 2
          this.listQuery.name = this.searchTable
          this.getqueryVip();
          break;
      }
    },
    btnaddBrand() {
      this.editSubjects = true
      this.dialogFormVisible = true
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryVip();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryVip();
    },
    Refresh() {
      this.listQuery = {
        startSize: 1,
        pageSize: 10,
        userId: localStorage.getItem("ID"),
        type: 1,
        name: undefined,
      }
      this.getqueryVip()
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = {
          vip: "",
          number: "",
          size: "",
          upgrade: "",
          name: ""
        }
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>